package com.redis.demo.controller;

import com.redis.demo.bean.Student;
import com.redis.demo.bean.StudentVo;
import com.redis.demo.service.StudentService;
import io.lettuce.core.RedisClient;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Time;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 *
 * @author Admin
 * @date 2024/6/14 14:06
 */
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/list")
    public List<Student> list(){
        List<Student> list = studentService.list();
            redisTemplate.opsForValue().set("skuId:list",list);
//        List<StudentVo> voList = list.stream().map(t -> {
//            StudentVo studentVo = new StudentVo();
//            BeanUtils.copyProperties(t, studentVo);
//            return studentVo;
//        }).collect(Collectors.toList());
        return list;
    }

    @GetMapping("/getById")
    public List<Student> getById(){
//        List<Student> student = (List<Student>) redisTemplate.opsForValue().get("skuId:list");
//        redisTemplate.expire("skuId:list",10, TimeUnit.SECONDS);
        List<Student> student = studentService.list();
//        Long expire = redisTemplate.opsForValue().getOperations().getExpire("skuId:list");
//        System.out.println("expire = " + expire);
        return student;
    }

//    @Scheduled(cron = "0/1 * * * * ?")
    public void watch() {
//        System.out.println("执行了");
        boolean flag;


        Object value = redisTemplate.opsForValue().get("skuId:" + 1);
        if (Objects.isNull(value)) {

        }
//        return "还未删除";
    }




    public static void main(String[] args) {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("输出了一次");
            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask,1,1000);
    }

}
